package com.example.demo.scheduledtask;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.example.demo.common.Constant;
import com.example.demo.config.ErpConfig;
import com.example.demo.mapper.AsyncWorkorderMapper;
import com.example.demo.mapper.PoMpsinfoMapper;
import com.example.demo.mapper.WmsPurchaseMapper;
import com.example.demo.utils.SoapXmlUtil;
import com.example.demo.vo.SellOutCallbackVo;
import com.example.demo.vo.SellOutListCallbackVo;
import com.example.demo.vo.SellOutListCallbackVoReq;
import com.example.demo.vo.XmlResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author : Zhang.yt
 * @date : 2024/7/29
 */
@Component
@Slf4j
@DS("wms")
public class SellOutCallback {
    @Resource
    private WmsPurchaseMapper wmsPurchaseMapper;

    @Autowired
    private SoapXmlUtil soapXmlUtil;

    @Autowired
    PoMpsinfoMapper poMpsinfoMapper;

    @Resource
    private ErpConfig erpConfig;



    /**
     * 销售出库回调
     */
//    @Scheduled(cron = "0/45 * * * * ?")//开启定时同步
    @Scheduled(fixedDelay = 9 * 1000)//开启定时同步
    //@Async(value = "taskExecutor")
    public void sellOutCallback() {

        //查询没有回调的数据
        SellOutCallbackVo sellOutCallbackVo = wmsPurchaseMapper.getSellOutCallback();
        if (ObjectUtils.isEmpty(sellOutCallbackVo.getXmdkud009())) {
            log.info("未获取到销售出库回写数据，修改回写报错数据重新回写");
            wmsPurchaseMapper.sellOutCallbackLose();
            return;
        }

        try {
            //捕获异常，设置单据状态为失败，避免总是取到该单据导致后续任务无法处理
            //超过20秒未获取到锁就退出
            //根据没有回调的数据查询细表
            List<SellOutListCallbackVo> sellOutListCallback = wmsPurchaseMapper.getSellOutListCallback(Arrays.asList(sellOutCallbackVo.getXmdkud009()));
            //
            sellOutCallbackVo.setActionType(Constant.ActionType_I);
            if (ObjectUtil.isEmpty(sellOutCallbackVo.getInbadocno())) {
                sellOutCallbackVo.setInbadocno(Constant.PMDSDOCNO_2401);
            }
            sellOutCallbackVo.setXmdksite(Constant.PMDSSITE_AMA);
            List<SellOutListCallbackVoReq> sellOutListCallbackVoReqList = BeanUtil.copyToList(sellOutListCallback, SellOutListCallbackVoReq.class);
            String resMessage = soapXmlUtil.getSoapXml("SalesInvoice", "xmdk_t", "xmdl_t", sellOutCallbackVo, sellOutListCallbackVoReqList);
            try {
                //转换为soap消息
                log.info("soap接口销售出库请求报文:{}", resMessage);
                //状态占用
                wmsPurchaseMapper.updateSellOutCallback(sellOutCallbackVo.getXmdkud009(), "erp待返回", 3);
                //发送soap消息，并解析结果
                String result;
                try {
                    result = soapXmlUtil.sendSoapMessageNew(resMessage, erpConfig.getUrl());
                } catch (Exception e) {
                    result = soapXmlUtil.sendSoapMessageNew(resMessage, erpConfig.URL2);
                }
                //调用等待
                if (sellOutListCallbackVoReqList.size() >= 50) {
                    Thread.sleep(40 * 1000);
//                    TimeUnit.SECONDS.sleep(60 * 1000);
                } else if (sellOutListCallbackVoReqList.size() >= 30) {
                    Thread.sleep(20 * 1000);
//                    TimeUnit.SECONDS.sleep(30 * 1000);
                } else if (sellOutListCallbackVoReqList.size() >= 10) {
                    Thread.sleep(5 * 1000);
//                    TimeUnit.SECONDS.sleep(5 * 1000);
                }
                log.info("soap接口销售出库回调报文->{}", result);
                XmlResp xmlResp = soapXmlUtil.analysisXml(result);
                if (xmlResp.getCode().equals("0")) {
                    //更新相应表状态为已下发
                    wmsPurchaseMapper.updateSellOutCallback(sellOutCallbackVo.getXmdkud009(), xmlResp.getData().get("xmdkdocno"), 1);
                } else {
                    //更新相应表状态为已下发
                    wmsPurchaseMapper.updateSellOutCallback(sellOutCallbackVo.getXmdkud009(), xmlResp.getMessage(), 2);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            wmsPurchaseMapper.updateSellOutCallback(sellOutCallbackVo.getXmdkud009(), "非常见的数据处理问题，请联系开发人员处理", 2);
            e.printStackTrace();
        }
    }

}
